package com.andy.dogcoin

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.andy.db.CoinInfo
import com.andy.db.CoinInfoDao
import com.andy.db.DBManager
import com.andy.utils.DateUtil
import com.andy.utils.UIUtil

/**
 * Created by Andy on  2024/4/24 16:01
 * Email: 963069079@qq.com
 *
 */
class AndyAddActivity() : AppCompatActivity()
{
    var coinAddr = ""
    var coinName = ""


    companion object{
        val ForResult = 1200
        val ForResult_Suc = 1201
        /**
         * id没有值的时候，必须传-1
         */
        fun Jump(context: Context, addr: String, name :String) {
            val intent = Intent(context, AndyAddActivity::class.java)
            intent.putExtra("coinAddr", addr)
            intent.putExtra("coinName", name)
            (context as Activity).startActivityForResult(intent, ForResult)
        }
    }



    override
    fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_andy_add)
        coinAddr = getIntent().getStringExtra("coinAddr")!!
        coinName = getIntent().getStringExtra("coinName")!!
        initView()
    }

    var add_addr_edt: EditText? = null
    var add_name_edt: EditText? = null
    var add_price_edt: EditText? = null
    var add_market_value_edt: EditText? = null
    var add_people_number_edt: EditText? = null
    var add_trade_number_edt: EditText? = null
    var add_trade_volume_edt: EditText? = null
    var txtId: TextView? = null
    var item: CoinInfo? = null
    open fun initView() {
        add_addr_edt = findViewById(R.id.add_addr_edt)
        add_name_edt = findViewById(R.id.add_name_edt)
        add_price_edt = findViewById(R.id.add_price_edt)
        add_market_value_edt = findViewById(R.id.add_market_value_edt)
        add_people_number_edt = findViewById(R.id.add_people_number_edt)
        add_trade_volume_edt = findViewById(R.id.add_trade_volume_edt)
        add_trade_number_edt = findViewById(R.id.add_trade_number_edt)

        if (coinAddr != "") {
//            setCoinData()
            add_addr_edt!!.setText(coinAddr)
            add_name_edt!!.setText(coinName)
        }
    }

//    open fun setCoinData() {
//        var dao = CoinInfoDao(this)
//        var mDBManager = DBManager.getInstance(this)
//        mDBManager.beginTransaction()
//        var result = false
//        try {
//            item = dao.queryByAddr(coinAddr)
//            result = true
//        } catch (e: Exception) {
//            e.printStackTrace()
//        } finally {
//            mDBManager.endTransaction(result)
//        }
//        if (item != null) {
////            add_addr_edt.setText(item.getAddr())
////            add_name_edt.setText(item.getCoin_name())
////            add_cost_edt.setText(item.getCost())
////            txtId!!.text = "" + item.getId()
//        }
//    }

    fun add_top_btn(v: View?) {}
    fun add_del_btn(v: View?) {
//        val id = txtId!!.text.toString()
//        if (id != "-1") {
//            val dao = CoinInfoDao(this)
//            val mDBManager = DBManager.getInstance(this)
//            mDBManager.beginTransaction()
//            var result = false
//            try {
//                dao.deleteById(id)
//                result = true
//            } catch (e: Exception) {
//                e.printStackTrace()
//            } finally {
//                mDBManager.endTransaction(result)
//            }
//            if (result) {
//                val intent = Intent()
//                // 设置返回码和返回携带的数据
//                setResult(ForResult_Suc, intent)
//                this.finish()
//            } else {
//                Toast.makeText(this, "操作失败", Toast.LENGTH_SHORT).show()
//            }
//        }
    }

    fun add_view_btn(v: View?) {
//        WebViewActivity2.start(this, add_addr_edt!!.text.toString(), -1)
    }

    fun add_add_btn(v: View?) {
         var address = add_addr_edt?.text
         if(address?.equals("") == true){
             UIUtil.showToast(this, "请输入合约地址")
             return
         }

        val mItem = CoinInfo()
        mItem.setCoin_name(add_name_edt!!.text.toString())
        mItem.setAddr(add_addr_edt!!.text.toString())
        mItem.setMarket_value(add_market_value_edt!!.text.toString())
        mItem.setPeople_number(add_people_number_edt!!.text.toString())
        mItem.setPrice(add_price_edt!!.text.toString())
        mItem.setTrade_number(add_trade_number_edt!!.text.toString())
        mItem.setTrade_volume(add_trade_volume_edt!!.text.toString())
        mItem.setSave_time(DateUtil.getStandardFormatTime(System.currentTimeMillis()).toString())

//        mItem.setCost(add_cost_edt!!.text.toString())
        val dao = CoinInfoDao(this)
        val mDBManager = DBManager.getInstance(this)
        mDBManager.beginTransaction()
        var result = false
        try {
//            if (id != "-1") {
//                mItem.setId(id.toInt())
//                dao.update(mItem)
//            } else {
                dao.insert(mItem)
//            }
            result = true
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            mDBManager.endTransaction(result)
        }
        if (result) {
            Toast.makeText(this, "操作suc", Toast.LENGTH_SHORT).show()
            val intent = Intent()
            // 设置返回码和返回携带的数据
            setResult(ForResult_Suc, intent)
            this.finish()
        } else {
            Toast.makeText(this, "操作失败", Toast.LENGTH_SHORT).show()
        }
    }
}